以下程式碼可以快速建立一個簡單 Alert
let alertController = UIAlertController(title: "title", message: "message", preferredStyle: UIAlertController.Style.alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: { action in
//按下OK後會做的事情
}))
present(alertController, animated: true, completion: nil)
如果要做多個按鈕,也可以寫成一個func,方便使用
func merge(titlemerge: String, stylemerge: UIAlertAction.Style ) {
alertController.addAction(UIAlertAction(title: titlemerge, style: stylemerge, handler: nil))
}
merge(titlemerge: "Ok" , stylemerge: UIAlertAction.Style.default)
merge(titlemerge: "Cancel" , stylemerge: UIAlertAction.Style.cancel)
merge(titlemerge: "Destructive" , stylemerge: UIAlertAction.Style.destructive)